#include "graphictile.h"

GraphicTile::GraphicTile(int w, int h)
{
    width = w;
    height = h;
    tile = NULL;
    this->setAcceptDrops(false);
    index = -1;
    transtype = 0;
    subindex = 0;
    setRect(0, 0, w, h);
//    setAcceptDrops(true);
    cblack = QColor(0,0,0);
    cgreen = QColor(0, 255, 0);
    pen.setColor(cblack);
}

GraphicTile::GraphicTile(QImage &img, int sx, int sy, int w, int h)
{
    width = w;
    height = h;
    transtype = 0;

    format = img.format();
    tile = new QImage(width, height, QImage::Format_ARGB32);

    this->setX(sx);
    this->setY(sy);
    QPainter painter(tile);
    painter.drawImage(0, 0, img, sx, sy, w, h);
//    tile = new QImage();
//    *tile = canvas.convertToFormat(format, colorTable, Qt::AutoColor);
//    QImage *nmg = tile->convertToFormat(format, colorTable, Qt::AutoColor).copy();
//    tile = nmg;
    setRect(0, 0, w, h);
    setFlag(ItemIsMovable);
    cblack = QColor(0,0,0);
    cgreen = QColor(0, 255, 0);
    pen.setColor(cgreen);
}

GraphicTile::~GraphicTile()
{
    delete tile;
}

void GraphicTile::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    if (tile!=NULL){
//        painter->drawPixmap(0, 0, *pixmap);
        painter->drawImage(0, 0, *tile);
    }
    painter->setPen(pen);
    painter->drawRect(0, 0, width, height);
}
